﻿Imports System.Data.SqlClient
Imports System.IO
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common


Public Class frmBUpandRestore

 
    Public Sub Backupdatabase(ByVal servername As String, ByVal userId As String, ByVal pass As String, ByVal databaseName As String, ByVal backupFile As String)

        Try
            Dim dbserver As Server
            'If (String.IsNullOrEmpty(userId) & String.IsNullOrEmpty(pass)) Then

            dbserver = New Server(New ServerConnection(servername))

            'Else
            'dbserver = New Server(New ServerConnection(servername, userId, pass))

            Dim dbbackup As New Backup()

            dbbackup.Action = BackupActionType.Database
            dbbackup.Database = databaseName
            dbbackup.Devices.AddDevice(backupFile, DeviceType.File)
            dbbackup.BackupSetName = "SMO Backup"
            dbbackup.BackupSetDescription = "SMO Backup"
            dbbackup.Initialize = True

            AddHandler dbbackup.PercentComplete, AddressOf ProgressEventHandler
            dbbackup.SqlBackup(dbserver)
            MessageBox.Show("Backup thành công!")
            'End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Lỗi")
        End Try
    End Sub

    Public Sub RestoreDatabase(ByVal servername As String, ByVal userId As String, ByVal pass As String, ByVal databaseName As String, ByVal restoreFile As String)

        Try
            Dim dbserver As Server
            'If (String.IsNullOrEmpty(userId) & String.IsNullOrEmpty(pass)) Then

            dbserver = New Server(New ServerConnection(servername))

            'Else
            'dbserver = New Server(New ServerConnection(servername, userId, pass))

            Dim dbrestore As New Restore()

            dbrestore.Action = BackupActionType.Database
            dbrestore.Database = databaseName
            dbrestore.Action = RestoreActionType.Database
            dbrestore.Devices.AddDevice(restoreFile, DeviceType.File)

            dbrestore.ReplaceDatabase = True

            dbrestore.PercentCompleteNotification = 10
            AddHandler dbrestore.PercentComplete, AddressOf ProgressEventHandler

            dbrestore.SqlRestore(dbserver)
            MessageBox.Show("Restore thành công!")
            'End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Lỗi")
        End Try
    End Sub



    Private Sub ProgressEventHandler(ByVal sender As System.Object, _
                                     ByVal e As PercentCompleteEventArgs)
        dbProgress.Value = e.Percent
        dbProgress.Update()
    End Sub

    Private Sub frmBUpandRestore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub


    Private Sub btnBackUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackUp.Click
        Backupdatabase(cmbServerName1.Text, txtUserName1.Text, txtPassword1.Text, cmbDataBase1.Text, tbBackupFile.Text)
    End Sub


    Private Sub btnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRestore.Click
        RestoreDatabase(cmbServerName1.Text, txtUserName1.Text, txtPassword1.Text, cmbDataBase1.Text, tbBackupFile.Text)
    End Sub
End Class